/**
 * 
 */
package com.goofengcn.website.application.util.mail;

import java.io.File;
import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * <p>
 * Title: 邮件的控制类
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2009
 * </p>
 * 
 * <p>
 * Company: 上海美华系统有限公司
 * </p>
 * 
 * @author Johnson
 * @version 1.0
 */
public class MailManager {

	private static final Log log = LogFactory.getLog(MailManager.class);

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String to_mail = "info@21shipping.com";
		String to_title = "title";
		String to_content = "content";
		// sendTxtMail(to_mail, to_title, to_content);
		String user_name = "";
		String temp_name = "";
		String user_hou = "@hotmail.com";
		char[] chars = new char[62];
		int i = 0;
		for (char a = '0'; a <= '9'; a++) {
			user_name = String.valueOf(a);
			temp_name += String.valueOf(user_name);
			// log.debug("temp_name == " + temp_name);
			chars[i] = a;
			i++;
		}
		for (char a = 'A'; a <= 'Z'; a++) {
			user_name = String.valueOf(a);
			temp_name += String.valueOf(user_name);
			// log.debug("temp_name == " + temp_name);
			chars[i] = a;
			i++;
		}
		for (char a = 'a'; a <= 'z'; a++) {
			user_name = String.valueOf(a);
			temp_name += String.valueOf(user_name);
			// log.debug("temp_name == " + temp_name);
			chars[i] = a;
			i++;
		}

		for (int n = 0; n < 62; n++) {
			// log.debug("chars[" + n + "] == " + chars[n]);
		}

		temp_name = "";

		File file = new File(MailManager.class.getResource("/")
				+ File.separator + "moban" + File.separator
				+ "accountactivate.txt");
		log.debug(file.exists());
		log.debug(file.getPath());
		// log.debug(System.getProperty("user.dir"));
		// log.debug(MailManager.class.getResource("/"));

		sendTxtMail("info@metinform.cn", "to_title", "to_content");
	}

	public static void sendTxtMail(String to_mail, String to_title,
			String to_content) {
		String host = MailConfig.getString("mail.smtp.host");
		String authname = MailConfig.getString("mail.smtp.username");
		String authpassword = MailConfig.getString("mail.smtp.password");
		Properties props = new Properties();
		if (host != null && !"".equals(host))
			props.put("mail.smtp.host", host);// 存储发送邮件服务器的信息
		else {
			log.debug("配置文件中host为空");
			return;
		}
		props.put("mail.smtp.auth", "true");// 同时通过验证
		Session s = Session.getInstance(props); // 根据属性新建一个邮件会话
		s.setDebug(false);// 调试
		// ----由邮件会话新建一个消息对象----
		MimeMessage message = new MimeMessage(s); // 由邮件会话新建一个消息对象
		// --------设置邮件--------
		try {
			InternetAddress from = new InternetAddress("info@21shipping.com");// 设置发件人
			message.setFrom(from);

			InternetAddress to = new InternetAddress(to_mail);
			message.setRecipient(Message.RecipientType.TO, to);// 设置收件人,并设置其接收类型为TO
			message.setSubject(to_title); // 设置主题
			message.setText(to_content); // 设置信件内容
			message.setSentDate(new Date()); // 设置发信时间
			// --------发送邮件--------
			message.saveChanges(); // 存储邮件信息
			Transport transport = s.getTransport("smtp");
			// log.debug("connect host == "+host);
			// log.debug("connect authname == "+authname);
			// log.debug("connect authpassword == "+authpassword);
			transport.connect(host, authname, authpassword);// --以smtp方式登录邮箱,第一个参数是发送邮件用的邮件服务器SMTP地址;第二个参数为用
			// 户名;第3个参数为密码
			transport.sendMessage(message, message.getAllRecipients());// 发送邮件,其中第二个参数是所有已设好的收件人地址
			transport.close();
		} catch (AddressException e1) {
			e1.printStackTrace();
		} catch (MessagingException e1) {
			e1.printStackTrace();
		}
	}
}